<template>
	<view class="container">
		<unicloud-db ref="DeltaUdb" v-slot:default="{data, loading, error, options}" :options="options"
			:collection="collectionList" :getone="true" loadtime="manual">
			<view v-if="error">{{error.message}}</view>
			<view v-else-if="loading">
				<uni-load-more :contentText="loadMore" status="loading"></uni-load-more>
			</view>
			<view v-else-if="data">
				<view class="u-flex u-flex-center">
					<text>{{data.title}}</text>
				</view>
				<view>
					<text>{{data.content}}</text>
				</view>
				<view v-if="data.attach&&data.attach.length">
					<view class="u-m-t-20">附件：</view>					
					<view class="u-p-l-40 u-m-t-30" hover-class="ha" v-for="(file, fileIndex) in data.attach" :key="fileIndex">
						<uni-icons type="paperclip" size="20"></uni-icons>
						<text class="u-cell-text p-text blue" @click="down(file.url)">
							{{file.name}}{{file.name.indexOf(".")==-1?"."+file.extname:""}}
						</text>
					</view>									
				</view>
				<view class="u-m-t-30" style="display: flex;justify-content: flex-end;">
					<uni-dateformat :threshold="[0, 0]" format="(yyyy/mm/dd)"
						style="color:#999;background-color: aliceblue;" :date="data.create_date"></uni-dateformat>

				</view>
			</view>
		</unicloud-db>
	</view>
</template>

<script>
	import {
		downword
	} from '@/js_sdk/downword.js';
	// 由schema2code生成，包含校验规则和enum静态数据
	import {
		enumConverter
	} from '@/js_sdk/validator/xasa-work.js'
	const db = uniCloud.databaseForJQL()

	export default {
		data() {
			return {
				id:'',
				loadMore: {
					contentdown: '',
					contentrefresh: '',
					contentnomore: ''
				},
				options: {
					// 将scheme enum 属性静态数据中的value转成text
					...enumConverter
				}
			}
		},
		onLoad(e) {
			this.id = e.id
		},
		computed: {
			collectionList() {
				return [
					db.collection('xasa-work').where('_id=="' + this.id + '"').field(
						'category_id,menu_id,title,content,attach,create_date').getTemp(),
					db.collection('xasa-categories').field('name, _id').getTemp(),
					db.collection('xasa-doc').where('enable==true').field('name, _id,extname,url').getTemp()
				]
			}
		},
		onReady() {
			if (this.id){
				this.$refs.DeltaUdb.loadData({clear: true})
			}
		},
		methods: {
			down(url) {
				//#ifdef H5
				if (plus) {
					plus.runtime.openFile(url, '', function(error) {
						console.log('打开外部链接失败：' + error.message);
					})
					//plus.runtime.openURL(url, function(error) {
					//	console.log('打开外部链接失败：' + error.message);
					//})
				}
				//#endif
				//#ifndef H5
				downword(url)
				//#endif
			}
		}
	}
</script>

<style lang="scss">
	.container {
		padding: 20px;
	}
	.blue {
		color: #35e;
	}

	.ha {
		color: #aaa;
	}
</style>